package com.ywu.config.center.standard.service;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.constant.CommonConstant;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.config.center.standard.data.ConfigCenterRepository;
import com.ywu.config.center.standard.dto.ConfigCenterDto;
import com.ywu.config.center.standard.entity.ConfigCenterEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName ConfigCenterService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/2 18:14
 * @Version 1.0
 **/
@Service
public class ConfigCenterService {
    @Autowired
    private ConfigCenterRepository configCenterRepository;

    /**
    * @Author GroundDemo
    * @Description 查询配置中心分页信息
    * @Date 18:14 2024/3/2
    * @Param [configCenterDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> getConfigInfo(ConfigCenterDto configCenterDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        configCenterDto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        // 查询分页数量
        Integer count = configCenterRepository.getConfigInfoCount(configCenterDto);
        PageService.execPageStartAndEnd(configCenterDto.getPageInfo());
        List<ConfigCenterEntity> entityList = configCenterRepository.getConfigInfo(configCenterDto);
        // 构建返回的分页对象
        PageList<ConfigCenterEntity> configCenterEntityPageList = new PageList<>();
        configCenterEntityPageList.setDataList(entityList);
        configCenterEntityPageList.setPageIndex(configCenterDto.getPageInfo().getPageIndex());
        configCenterEntityPageList.setPageSize(configCenterDto.getPageInfo().getPageSize());
        configCenterEntityPageList.setRecordCount(count);
        return CommonResult.success(configCenterEntityPageList);
    }

    /**
    * @Author GroundDemo
    * @Description 添加配置
    * @Date 7:55 2024/3/3
    * @Param [configCenterDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> addConfigInfo(ConfigCenterDto configCenterDto) {
        ConfigCenterEntity configCenterEntity = new ConfigCenterEntity();
        BeanUtils.copyProperties(configCenterDto, configCenterEntity);
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        configCenterEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
        configCenterEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        configCenterRepository.addConfigInfo(configCenterEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 修改配置
    * @Date 8:00 2024/3/3
    * @Param [configCenterDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> updateConfigInfo(ConfigCenterDto configCenterDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 校验当前配置是否存在
        configCenterDto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        ConfigCenterEntity checkRes = configCenterRepository.checkConfigInfo(configCenterDto);
        if (Objects.isNull(checkRes) || Objects.isNull(checkRes.getId())) {
            return CommonResult.error().setMessage("config not exist").setCode(CommonConstant.ERROR_CODE);
        }
        ConfigCenterEntity configCenterEntity = new ConfigCenterEntity();
        BeanUtils.copyProperties(configCenterDto, configCenterEntity);
        configCenterEntity.updateInfo(currentContext.getYwuContextUser().getUserAccount());
        configCenterRepository.updateConfigInfo(configCenterEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除配置
    * @Date 8:03 2024/3/3
    * @Param [configCenterDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteConfigInfo(ConfigCenterDto configCenterDto) {
        if (Objects.isNull(configCenterDto.getId())) {
            return CommonResult.success(null);
        }
        // 校验当前配置是否存在
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        configCenterDto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        ConfigCenterEntity checkRes = configCenterRepository.checkConfigInfo(configCenterDto);
        if (Objects.isNull(checkRes) || Objects.isNull(checkRes.getId())) {
            return CommonResult.error().setMessage("config not exist").setCode(CommonConstant.ERROR_CODE);
        }
        configCenterRepository.deleteConfigInfo(configCenterDto);
        return CommonResult.success(null);
    }
}
